import math
def getAQI(data2):
    iaqipm25 = IAQIPM25(data2['dayPM25'])
    iaqiso2 = IAQISO2(data2['daySO2'])
    iaqino2 = IAQINO2(data2['dayNO2'])
    iaqipm10 = IAQIPM10(data2['dayPM10'])
    iaqico = IAQICO(data2['dayCO'])
    iaqio3 = IAQIO3(data2['dayO3'])
    return max(iaqipm25,iaqiso2,iaqino2,iaqipm10,iaqico,iaqio3)

def getAQIList(data2):
    iaqipm25 = math.ceil(IAQIPM25(data2['dayPM25'])*10000)/10000
    iaqiso2 = math.ceil(IAQISO2(data2['daySO2'])*10000)/10000
    iaqino2 = math.ceil(IAQINO2(data2['dayNO2'])*10000)/10000
    iaqipm10 = math.ceil(IAQIPM10(data2['dayPM10'])*10000)/10000
    iaqico = math.ceil(IAQICO(data2['dayCO'])*10000)/10000
    iaqio3 = math.ceil(IAQIO3(data2['dayO3'])*10000)/10000
    return {'pm25':iaqipm25,'so2':iaqiso2,'no2':iaqino2,'pm10':iaqipm10,'co':iaqico,'o3':iaqio3}

def getWRW(data2):
    lists = getAQIList(data2)
    firstWR = 0
    WR = '无'
    for i in lists:
        if lists[i] > 100 and firstWR < lists[i]:
            firstWR = lists[i]
            WR=i
    return WR

def chaobiao(data2):
    lists = getAQIList(data2)
    CB = ''
    for i in lists:
        if lists[i] > 150:
            CB += i+','
    if(CB == ''):
        CB = '无'
    CB = CB.rstrip(',')
    return CB
def grade(data2):
    aqi = getAQI(data2)
    if(aqi <= 50):
        grade = '优'
        level = '一级'
        suggest = '空气质量令人满意，基本无空气污染'
        measure = '各类人群可正常活动'
    elif(aqi > 50 and aqi <=100):
        grade = '良'
        level = '二级'
        suggest = '空气质量可接受，但某些污染物可能对极少数异常敏感人群健康有较弱影响'
        measure = '极少数异常敏感人群应减少户外活动'
    elif(aqi > 100 and aqi <=150):
        grade = '轻度污染'
        level = '三级'
        suggest = '易感人群症状有轻度加剧，健康人群出现刺激症状'
        measure = '儿童，老年人及心脏病，呼吸系统疾病患者应减少长时间，高强度的户外锻炼'
    elif(aqi > 150 and aqi <=200):
        grade = '中度污染'
        level = '四级'
        suggest = '进一步加剧易感人群症状，可能对正常人群心脏，呼吸系统有影响'
        measure = '儿童，老年人及心脏病，呼吸系统疾病患者避免长时间，高强度的户外锻炼，一般人群适量减少户外运动'
    elif(aqi > 201 and aqi <=300):
        grade = '重度污染'
        level = '五级'
        suggest = '心脏病和肺病患者症状显著加剧，运动耐受力降低，健康人群普遍出现症状'
        measure = '儿童，老年人及心脏病，肺病患者应停留在室内，停止户外运动，一般人群减少户外运动'
    elif(aqi > 300):
        grade = '严重污染'
        level = '六级'
        suggest = '健康人群运动耐受力降低，有明显强烈症状，提前出现某些疾病'
        measure = '儿童，老年人和病人应当留在室内，避免体力消耗，一般人群应避免户外活动'
    return {'grade':grade,'level':level,'suggest':suggest,'measure':measure}
def IAQIPM25(pm25):
    if pm25 == '':
        return 0
    if pm25>=0 and pm25 < 35:
        IAQIPM25 = (50/35)*(pm25)
    elif pm25 >=35 and pm25 < 75:
        IAQIPM25 = ((100-50)/(75-35))*(pm25-35)+50
    elif pm25 >= 75 and pm25 < 115 :
        IAQIPM25 = ((150-100)/(150-115))*(pm25-75)+100
    elif pm25 >= 115 and pm25 < 150:
        IAQIPM25 = ((200-150)/(150-115))*(pm25-115)+150
    elif pm25 >= 150 and pm25 <250:
        IAQIPM25 = ((300-200)/(250-150))*(pm25-150)+200
    elif pm25 >= 250 and pm25 < 350:
        IAQIPM25 = ((400-300)/(350-250))*(pm25-250)+250
    elif pm25 >= 350 and pm25 < 500:
        IAQIPM25 = ((500-400)/(500-350))*(pm25-350)+400
    else:
        IAQIPM25 = 0
    return IAQIPM25
def IAQISO2(so2):
    if so2 == '':
        return 0
    if so2>=0 and so2 < 50:
        IAQISO2 = (50/50)*(so2)
    elif so2 >=50 and so2 < 150:
        IAQISO2 = ((100-50)/(150-50))*(so2-50)+50
    elif so2 >= 150 and so2 < 475 :
        IAQISO2 = ((150-100)/(475-150))*(so2-150)+100
    elif so2 >= 475 and so2 < 800:
        IAQISO2 = ((200-150)/(800-475))*(so2-475)+150
    elif so2 >= 150 and so2 <250:
        IAQISO2 = ((300-200)/(250-150))*(so2-150)+200
    elif so2 >= 250 and so2 < 350:
        IAQISO2 = ((400-300)/(350-250))*(so2-250)+250
    elif so2 >= 350 and so2 < 500:
        IAQISO2 = ((500-400)/(500-350))*(so2-350)+400
    else:
        IAQISO2 = 0
    return IAQISO2
def IAQINO2(no2):
    if no2 == '':
        return 0
    if no2>=0 and no2 < 40:
        IAQINO2 = (50/40)*(no2)
    elif no2 >=40 and no2 < 80:
        IAQINO2 = ((100-50)/(80-40))*(no2-40)+50
    elif no2 >= 80 and no2 < 180 :
        IAQINO2 = ((150-100)/(180-80))*(no2-80)+100
    elif no2 >= 180 and no2 < 280:
        IAQINO2 = ((200-150)/(280-180))*(no2-180)+150
    elif no2 >= 280 and no2 <565:
        IAQINO2 = ((300-200)/(565-280))*(no2-280)+200
    elif no2 >= 565 and no2 < 750:
        IAQINO2 = ((400-300)/(750-565))*(no2-565)+250
    elif no2 >= 750 and no2 < 940:
        IAQINO2 = ((500-400)/(940-750))*(no2-750)+400
    else:
        IAQINO2 = 0
    return IAQINO2
def IAQIPM10(pm10):
    if pm10 == '':
        return 0
    if pm10>=0 and pm10 < 50:
        IAQIPM10 = (50/50)*(pm10)
    elif pm10 >=50 and pm10 < 150:
        IAQIPM10 = ((100-50)/(150-50))*(pm10-50)+50
    elif pm10 >= 150 and pm10 < 250 :
        IAQIPM10 = ((150-100)/(250-150))*(pm10-150)+100
    elif pm10 >= 250 and pm10 < 350:
        IAQIPM10 = ((200-150)/(350-250))*(pm10-250)+150
    elif pm10 >= 350 and pm10 <420:
        IAQIPM10 = ((300-200)/(420-350))*(pm10-350)+200
    elif pm10 >= 420 and pm10 < 500:
        IAQIPM10 = ((400-300)/(500-420))*(pm10-420)+250
    elif pm10 >= 500 and pm10 < 600:
        IAQIPM10 = ((500-400)/(600-500))*(pm10-500)+400
    else:
        IAQIPM10 = 0
    return IAQIPM10
def IAQICO(co):
    if co == '':
        return 0
    if co>=0 and co < 2:
        IAQICO = (2/50)*(co)
    elif co >=2 and co < 4:
        IAQICO = ((100-50)/(4-2))*(co-2)+50
    elif co >= 4 and co < 14 :
        IAQICO = ((150-100)/(14-4))*(co-4)+100
    elif co >= 14 and co < 24:
        IAQICO = ((200-150)/(24-14))*(co-14)+150
    elif co >= 24 and co <36:
        IAQICO = ((300-200)/(36-24))*(co-24)+200
    elif co >= 36 and co < 48:
        IAQICO = ((400-300)/(48-36))*(co-36)+250
    elif co >= 48 and co < 60:
        IAQICO = ((500-400)/(60-48))*(co-48)+400
    else:
        IAQICO = 0
    return IAQICO
def IAQIO3(o3):
    if o3 == '':
        return 0
    if o3>=0 and o3 < 100:
        IAQIO3 = (100/50)*(o3)
    elif o3 >=100 and o3 < 160:
        IAQIO3 = ((100-50)/(160-100))*(o3-100)+50
    elif o3 >= 160 and o3 < 215 :
        IAQIO3 = ((150-100)/(215-160))*(o3-160)+100
    elif o3 >= 215 and o3 < 260:
        IAQIO3 = ((200-150)/(265-215))*(o3-215)+150
    elif o3 >= 260 and o3 <800:
        IAQIO3 = ((300-200)/(800-260))*(o3-260)+200
    else:
        IAQIO3 = 0
    return IAQIO3